<?php
/**
 *  用户模型 .
 * User: myl
 */
namespace app\api\model;

use think\Model;
use think\Db;

class UserModel extends Model
{
    /**
     * 添加一行信息
     * @param $map
     * @return bool
     */
    public static function increase($map){
        $user = Db::name('users')->insert($map);
        return $user;
    }

    /**
     * 按条件查询（单条数据）
     * @param $where
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function whereGetOne($where){
        $user = Db::name('users')->where($where)->find();
        return $user;
    }

    /**
     * 按条件查询（多条数据）
     * @param $where
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function whereGetMany($where){
        $user = Db::name('users')->where($where)->select();
        return $user;
    }

    /**
     * 按条件修改信息
     * @param $where
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function modify($where,$data){
        $user = Db::name('users')->where($where)->update($data);
        return $user;
    }

    /**
     * 记录签到，增加用户积分（2分）
     * @param $uid
     * @param $data
     * @return int
     */
    public static function signAdd($uid,$data)
    {
        $time = date("Y-m-d",time()); //今天
        $yesterday = date("Y-m-d",strtotime("-1 day")); //昨天

        $row = M('user_sign')->field("FROM_UNIXTIME(sign_last,'%Y-%m-%d') as sign_last")->where("user_id",$uid)->order('sign_last desc')->find();
        if ($row['sign_last'] == $time) {
            return 1; //今天已签到
        } else {
            $arr = M('user_sign')->field("FROM_UNIXTIME(sign_last,'%Y-%m-%d') as signLast,sign_count")->where("user_id",$uid)->order('sign_last desc')->find();
            //判断是否是连续签到
            if ($arr['signLast'] == $yesterday) {
                $data['sign_count'] = $arr['sign_count'] + 1;
            } else {
                $data['sign_count'] = 1;
            }
            $rs = M('user_sign')->insert($data);
            $user = M('users')->field('pay_points')->where("user_id",$uid)->find();
            $pay_points = $user['pay_points'] + 2;
            M('users')->where("user_id",$uid)->update(['pay_points'=>$pay_points]); //增加积分（2分）
            if ($rs) {
                return 2; //签到成功
            } else {
                return 3; //签到失败
            }
        }
    }

    /**
     * 设置支付密码 and 重置密码
     * @param $uid
     * @param $pwd
     * @param $npwd
     * @param $mobile
     * @param $type
     * @return int
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function setPaymentPassword($uid,$pwd,$npwd,$mobile,$type)
    {
        $userData = Db::name('users')->where(['user_id' => $uid, 'mobile' => $mobile])->find();
        if (!empty($userData)) {
            if ($type==1) {
                $user = Db::name('users')->where('user_id', $uid)->update(['paypwd' => $pwd]);
                if (!empty($user)) {
                    return 1; //成功
                } else {
                    return 0; //失败
                }
            }
            if ($type == 2) {
                if ($pwd == $npwd) {
                    return 4; //新密码不能和旧密码一样
                }
                $userPwd = Db::name('users')->where(['user_id' => $uid, 'password' => $npwd])->find(); //判断旧密码是否正确
                if (!empty($userPwd)) {
                    $user = Db::name('users')->where('user_id', $uid)->update(['paypwd' => $pwd]);
                    if (!empty($user)) {
                        return 1; //成功
                    } else {
                        return 0; //失败
                    }
                } else {
                    return 2; //旧密码错误
                }
            }
        } else {
            return 3; //手机号码错误
        }
    }

    /**
     * 获取充值单号 order_sn
     * @return string
     */
    public static function get_order_sn()
    {
        $order_sn = null;
        // 保证不会有重复订单号存在
        while(true){
            $order_sn = date('YmdHis').rand(1000,9999); // 订单编号
            $order_sn_count = M('recharge')->where("order_sn = ".$order_sn)->count();
            if($order_sn_count == 0)
                break;
        }

        return $order_sn;
    }

}